﻿using BusinessLogic;
using Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AppAdmin.Controllers
{
    public class PicUploadController : Controller
    {
        //
        // GET: /PicUpload/


        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Summit()
        {
            return Json(new { });
        }

        [HttpPost]
        public ActionResult BatchUpload(string Source)
        {
            bool isSavedSuccessfully = true;
            int count = 0;
            string msg = "";
            string type = "";
            if (Source == "1")
            {
                type = "Shop";
            }
            else if (Source == "2")
            {
                type = "Stylist";
            }
            List<string> HouZui = new List<string>() { ".png", ".jpg", ".jpeg", ".bmp" };
            List<string> ResultUrl = new List<string>();

            string fileName = "";
            string fileExtension = "";
            string filePath = "";
            string fileNewName = "";

            //这里是获取隐藏域中的数据
            //int albumId = string.IsNullOrEmpty(Request.Params["hidAlbumId"]) ?
            //    0 : int.Parse(Request.Params["hidAlbumId"]);

            try
            {
                string directoryPath = Server.MapPath("~/UploadPic/photos");
                string directorytem = "/" + type + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
                if (!Directory.Exists(directoryPath + directorytem))
                    Directory.CreateDirectory(directoryPath + directorytem);

                foreach (string f in Request.Files)
                {
                    HttpPostedFileBase file = Request.Files[f];

                    if (file != null && file.ContentLength > 0)
                    {
                        fileName = file.FileName;
                        fileExtension = Path.GetExtension(fileName);
                        if (!HouZui.Contains(fileExtension.Trim()))
                        {
                            continue;
                        }
                        fileNewName = Guid.NewGuid().ToString() + fileExtension;

                        filePath = Path.Combine(directoryPath + directorytem, fileNewName);
                        file.SaveAs(filePath);
                        ResultUrl.Add(directorytem + fileNewName);
                        count++;
                    }
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                isSavedSuccessfully = false;
            }

            return Json(new
            {
                ResultUrl = ResultUrl,
                Result = isSavedSuccessfully,
                Count = count,
                Message = msg
            });
        }

        /// <summary>
        /// 1.店面2.发型师
        /// </summary>
        /// <param name="itemId"></param>
        /// <param name="SourceType"></param>
        /// <returns></returns>
        public ActionResult GetPicListByByTypeAndSource(string itemId, string SourceType)
        {
            var data = new PicLogic().GetPicListByTypeAndSource(Convert.ToInt32(itemId), Convert.ToInt32(SourceType));
            return Content(data.ToJson());
        }

        public ActionResult DelPic(string url)
        {
            try
            {
                new PicLogic().DeletePic(url);
                return Content(new AjaxResult { state = ResultType.success.ToString(), message = "操作成功。" }.ToJson());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public ActionResult Upload()
        {
            //HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
            //string imgPath = "";
            //if (hfc.Count > 0)
            //{
            //    imgPath = "/testUpload" + hfc[0].FileName;
            //    string PhysicalPath = Server.MapPath(imgPath);
            //    hfc[0].SaveAs(PhysicalPath);
            //}
            //return Content(imgPath);

            #region 处理图片
            string type = "Shop";
            string fileName = "";
            string fileExtension = "";
            string filePath = "";
            string fileNewName = "";
            List<string> HouZui = new List<string>() { ".png", ".jpg", ".jpeg", ".bmp" };
            string resultUrl = "";

            string directoryPath = Server.MapPath("~/UploadPic/photos");
            string directorytem = "/" + type + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
            if (!Directory.Exists(directoryPath + directorytem))
                Directory.CreateDirectory(directoryPath + directorytem);
            HttpPostedFileBase file = Request.Files[0];

            if (file != null && file.ContentLength > 0)
            {
                fileName = file.FileName;
                fileExtension = Path.GetExtension(fileName);
                if (HouZui.Contains(fileExtension.Trim()))
                {
                    fileNewName = Guid.NewGuid().ToString() + fileExtension;

                    filePath = Path.Combine(directoryPath + directorytem, fileNewName);
                    file.SaveAs(filePath);
                    resultUrl = directorytem + fileNewName;
                }
            }
            return Content(new { imgurl = resultUrl }.ToJson());
            #endregion
        }

        public ActionResult UploadStylistPic()
        {
            #region 处理图片
            string type = "Stylist";
            string fileName = "";
            string fileExtension = "";
            string filePath = "";
            string fileNewName = "";
            List<string> HouZui = new List<string>() { ".png", ".jpg", ".jpeg", ".bmp" };
            string resultUrl = "";

            string directoryPath = Server.MapPath("~/UploadPic/photos");
            string directorytem = "/" + type + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
            if (!Directory.Exists(directoryPath + directorytem))
                Directory.CreateDirectory(directoryPath + directorytem);
            HttpPostedFileBase file = Request.Files[0];

            if (file != null && file.ContentLength > 0)
            {
                fileName = file.FileName;
                fileExtension = Path.GetExtension(fileName);
                if (HouZui.Contains(fileExtension.Trim()))
                {
                    fileNewName = Guid.NewGuid().ToString() + fileExtension;

                    filePath = Path.Combine(directoryPath + directorytem, fileNewName);
                    file.SaveAs(filePath);
                    resultUrl = directorytem + fileNewName;
                }
            }
            return Content(new { imgurl = resultUrl }.ToJson());
            #endregion
        }

        public ActionResult UploadNewsPic()
        {
            //HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
            //string imgPath = "";
            //if (hfc.Count > 0)
            //{
            //    imgPath = "/testUpload" + hfc[0].FileName;
            //    string PhysicalPath = Server.MapPath(imgPath);
            //    hfc[0].SaveAs(PhysicalPath);
            //}
            //return Content(imgPath);

            #region 处理图片
            string type = "News";
            string fileName = "";
            string fileExtension = "";
            string filePath = "";
            string fileNewName = "";
            List<string> HouZui = new List<string>() { ".png", ".jpg", ".jpeg", ".bmp" };
            string resultUrl = "";

            string directoryPath = Server.MapPath("~/UploadPic/photos");
            string directorytem = "/" + type + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
            if (!Directory.Exists(directoryPath + directorytem))
                Directory.CreateDirectory(directoryPath + directorytem);
            HttpPostedFileBase file = Request.Files[0];

            if (file != null && file.ContentLength > 0)
            {
                fileName = file.FileName;
                fileExtension = Path.GetExtension(fileName);
                if (HouZui.Contains(fileExtension.Trim()))
                {
                    fileNewName = Guid.NewGuid().ToString() + fileExtension;

                    filePath = Path.Combine(directoryPath + directorytem, fileNewName);
                    file.SaveAs(filePath);
                    resultUrl = directorytem + fileNewName;
                }
            }
            return Content(new { imgurl = resultUrl }.ToJson());
            #endregion
        }
    }
}
